package com.lvmama.controller.dw;

import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.lvmama.client.dw.RecommendClient;
import com.lvmama.common.enums.CategoryEnum;
import com.lvmama.common.enums.PageTypeEnum;
import com.lvmama.common.enums.PlatformEnum;
import com.lvmama.common.result.CodeMessage;
import com.lvmama.common.result.Result;
import com.lvmama.common.utils.ExportCSVUtils;
import com.lvmama.pojo.dto.dw.RecommendAnalysisQuery;
import com.lvmama.pojo.entity.dw.recommend.Condition;
import com.lvmama.pojo.entity.dw.recommend.OverView;
import com.lvmama.pojo.entity.dw.recommend.RecommendAnalysis;
import com.lvmama.pojo.entity.dw.recommend.Trend;

/**
 * @param
 * @Title:
 * @Author:wangpeng
 * @Description: TODO(这里用一句话描述这个方法的作用)
 * @return
 * @throws
 */
@Slf4j
@Controller
@RequestMapping(value = "/recommend")
public class RecommendController {

	@Autowired
	RecommendClient client;

	/**
	 * 推荐展位报告页面
	 * @return
	 */
	@RequestMapping(value = "/booth",method = RequestMethod.GET)
	public String booth(){
		return "dw/recommend/recommendBooth";
	}

	/**
	 * 推荐效果报告页面
	 * @return
	 */
	@RequestMapping(value = "/effect",method = RequestMethod.GET)
	public String effect(){
		return "dw/recommend/recommendEffect";
	}

	/**
	 * 页面的查询条件
	 * @return
	 */
	@RequestMapping(value = "/condition",method = RequestMethod.GET)
	@ResponseBody
	public Condition condition(){
		Condition condition = new Condition();
		try{
			List<String> category = new ArrayList<>();
			List<String> pagetype = new ArrayList<>();
			List<String> platform = new ArrayList<>();

			for(CategoryEnum e : CategoryEnum.values())
				category.add(e.getCategoryName());
			for(PlatformEnum p: PlatformEnum.values())
				platform.add(p.getPlatform());
			for(PageTypeEnum t: PageTypeEnum.values())
				pagetype.add(t.getTypeName());

			condition.setCategory(category);
			condition.setPagetype(pagetype);
			condition.setPlatform(platform);
		}catch (Exception e){
			log.error("condition error:",e.getMessage(),e);
		}
		return condition;
	}

	/**
	 * 业务：数据概览
	 * 根据条件查询 ->数据概览的数据
	 * @param raDTO
	 * @return OverView
	 */
	@RequestMapping(value = "/overview",method = RequestMethod.GET)
	@ResponseBody
	public Result<OverView> overview(RecommendAnalysisQuery raDTO){
		log.info("overview method start");
		try {
			log.info("overview method end");
			return Result.success(client.overview(raDTO));
		}catch (Exception e){
			log.error("overview",e.getMessage(),e);
			return Result.error(CodeMessage.SERVER_ERROR);
		}
		
	}

	/**
	 * 业务：数据趋势
	 * 根据条件查询 ->数据按照日期进行分组统计,得到每一日的点击率,转换率等数据
	 * @param raDTO
	 * @return Trend
	 */
	@RequestMapping(value = "/trend",method = RequestMethod.GET)
	@ResponseBody
	public Result<Trend> trend(RecommendAnalysisQuery raDTO){
		try {
			return Result.success(client.trend(raDTO));
		}catch (Exception e){
			log.error("trend",e.getMessage(),e);
			return Result.error(CodeMessage.SERVER_ERROR);
		}
	}

	/**
	 * 业务：明细指标表格数据
	 * 根据条件查询 ->查询数据库中的原始数据明细
	 * @param raDTO
	 * @return PageInfo<RecommendAnalysis>
	 */
	@RequestMapping(value = "/detail",method = RequestMethod.POST)
	@ResponseBody
	public Result<PageInfo<RecommendAnalysis>> detailList(RecommendAnalysisQuery raDTO){
		try {
			return Result.success(client.detailList(raDTO));
		}catch (Exception e){
			log.error("detailList",e.getMessage(),e);
			return Result.error(CodeMessage.SERVER_ERROR);
		}
	}

	/**
	 * 根据条件导出csv的数据
	 * @param raDTO
	 * @return
	 */
	@RequestMapping(value = "exportCSV",method = RequestMethod.POST)
	public void export(RecommendAnalysisQuery raDTO, HttpServletResponse response) {
		/**
		 * 强制设置,只返回2万条数据
		 */
		raDTO.setExportLimit(2000);
		try {
			List<RecommendAnalysis> exportList = client.exportList(raDTO);
			/**
			 * 将产品名称为空的数据设置为字符串的空值
			 */
			exportList.stream().forEach(s ->{ if(s.getProductName() == null) s.setProductName(""); });
			String fileName = "看板数据" + raDTO.getStartday() + "-" + raDTO.getEndday();
			response.setContentType("application/vnd.ms-excel");
			fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
			response.setHeader("content-disposition", "attachment;filename=" + fileName + ".csv");
			BufferedWriter bf = new BufferedWriter(new OutputStreamWriter(response.getOutputStream(), "gbk"), 1024);
			ExportCSVUtils.exportCSV(exportList, bf, "yyyy-MM-dd HH:mm:ss");
		}catch (Exception e){
			log.error("exportCSV error :",e.getMessage(),e);
		}
	}

	
}
